package com.leyou.search.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.search.pojo.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.search.utils.SearchRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 搜索页面的分页查询
     *
     * @param searchRequest  每页的容量
     * @return  返回PageResult<Goods>
     */
    @PostMapping("page")
    public ResponseEntity<PageResult<Goods>> search(@RequestBody SearchRequest searchRequest) {
        PageResult<Goods> goodsPageResult = this.searchService.search(searchRequest);
        if (goodsPageResult != null && goodsPageResult.getItems() != null && goodsPageResult.getItems().size() != 0) {
            return ResponseEntity.status(HttpStatus.OK).body(goodsPageResult);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
